//
//  MCAddCreatePlanVc.swift
//  MCBodyBuildingPro
//
//  Created by Renlvshijie on 2023/5/19.
//

import UIKit
import ObjectMapper
import QMUIKit
import MJRefresh
import SnapKit
import Toast_Swift
import Kingfisher


class MCAddCreatePlanVc: MCBaseVc {
    
    let addCreatePlanLeftTableViewIdentifier = "addCreatePlanLeftTableViewIdentifier"
    let addCreatePlanRightTableViewIdentifier = "addCreatePlanRightTableViewIdentifier"
    
    
    @IBOutlet weak var addPlanLeftTableView: UITableView!
    @IBOutlet weak var addPlanRightTableView: UITableView!
    

        
    private var leftCreateDataSource:Array = [String]()
    private var current:Int = 1
    private var dataSource:Array = [HomeRecommandRecord?]()
    
    private var positions:String?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.qmui_color(withHexString: "#F7F7F9")
        self.navigationItem.title = "全部计划"
        addPlanLeftTableView.dataSource = self
        addPlanLeftTableView.delegate = self
        
        addPlanRightTableView.dataSource = self
        addPlanRightTableView.delegate = self
        
        self.addPlanLeftTableView.register(MCLeftTableCell.self, forCellReuseIdentifier: addCreatePlanLeftTableViewIdentifier)
        self.addPlanLeftTableView.rowHeight = 50
        
        self.addPlanRightTableView.rowHeight = 84
        self.addPlanRightTableView.register(UINib(nibName: "MCPlanTableCell", bundle: Bundle.main), forCellReuseIdentifier: addCreatePlanRightTableViewIdentifier)
        
        //左边数据
        self.getLeftData()
        
        self.addPlanRightTableView.mj_header = MJRefreshNormalHeader.init(refreshingBlock: {
            self.current = 1
            self.getRightListData(current: self.current,positions: self.positions)
        })
        
        self.addPlanRightTableView.mj_header?.beginRefreshing()
        
        
        self.addPlanRightTableView.mj_footer = MJRefreshAutoNormalFooter.init(refreshingBlock: {
            self.getRightListData(current: self.current,positions: self.positions)
        })
        
    }
        
    //左边部分
    func getLeftData() -> Void {
        NetWorkRequest(MCActionViewModel.getExercisePosition) { ResponseModel in
            if ResponseModel.code == 200 {
                let bodyList  = Mapper<MCActionbodyModel>().mapArray(JSONString: ResponseModel.dataString)!
                
                self.leftCreateDataSource = (bodyList.map({ actionbodyModel in
                    return actionbodyModel.dictValue ?? ""
                }))
                self.leftCreateDataSource.insert("全部", at: 0)
                self.addPlanLeftTableView.reloadData()
                self.addPlanLeftTableView.reloadData()
                self.addPlanLeftTableView.selectRow(at: IndexPath(item: 0, section: 0), animated: true, scrollPosition: .none)
                self.addPlanLeftTableView.delegate?.tableView?(self.addPlanLeftTableView, didSelectRowAt: IndexPath(item: 0, section: 0))
            }else{
                self.view.makeToast("网络失败",position: .center)
            }
        } failureCallback: { ResponseModel in
            self.view.makeToast("网络失败",position: .center)
        }
    }
    
    
    func getRightListData(current:Int?,positions:String?) -> Void {
        NetWorkRequest(MCAddCreateApi.recommandPlan(current: current, position: positions ?? "")) { ResponseModel in
            self.addPlanRightTableView.mj_footer?.endRefreshing()
            self.addPlanRightTableView.mj_header?.endRefreshing()
            let createModel = HomeRecommandInfoListModel.init(JSONString: ResponseModel.dataString)
            if current == 1 {
                self.dataSource.removeAll()
                self.dataSource.append(contentsOf: createModel?.records ?? [])
                
            }else{
                self.current += 1
                
                self.dataSource.append(contentsOf: createModel?.records ?? [])
            }
            self.addPlanRightTableView.reloadData()
        } failureCallback: { ResponseModel in
            self.addPlanRightTableView.mj_footer?.endRefreshing()
            self.addPlanRightTableView.mj_header?.endRefreshing()
            self.view.makeToast("网络失败",position: .center)
        }
    }
    
    
    
}

extension MCAddCreatePlanVc:UITableViewDelegate,UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if tableView == addPlanLeftTableView {
            return self.leftCreateDataSource.count
        }else if tableView == addPlanRightTableView {
            return self.dataSource.count
        }else {
            return 0
        }
        
        
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if tableView == addPlanLeftTableView {
            let cell:MCLeftTableCell = tableView.dequeueReusableCell(withIdentifier: addCreatePlanLeftTableViewIdentifier, for: indexPath) as! MCLeftTableCell
            let leftTitleLabelModel = self.leftCreateDataSource[indexPath.row]
            cell.titleInfLabel.text = leftTitleLabelModel
            
            return cell
        }else {
            let cell:MCPlanTableCell = tableView.dequeueReusableCell(withIdentifier: addCreatePlanRightTableViewIdentifier, for: indexPath) as! MCPlanTableCell
            
            let records = self.dataSource[indexPath.row]
            
            cell.HeaderIconImageView.kf.setImage(with:URL(string: records?.planUrl ?? ""))
            cell.titleInfoLabel.text = records?.planName ?? ""
            
            
           for subView  in cell.LabelLayoutView.subviews {
               subView.removeFromSuperview()
            }
            
            
            let paddingLabel = QMUIButton.init()
            paddingLabel.backgroundColor = UIColor.qmui_color(withHexString:"#35DE94")?.withAlphaComponent(0.1)
            paddingLabel.setTitleColor( UIColor.qmui_color(withHexString: "#35DE94"), for: .normal)
            paddingLabel.setTitle("L\(String(describing:(records?.planLevel ?? 0)))", for: .normal)
            paddingLabel.contentEdgeInsets = UIEdgeInsets(top: 5, left: 10, bottom: 5, right: 10)
            paddingLabel.layer.cornerRadius = 5
            paddingLabel.titleLabel?.font = UIFont(name: "PingFangSC-Regular", size: 10)
            paddingLabel.layer.masksToBounds = true
            cell.LabelLayoutView.addSubview(paddingLabel)
            
           return cell
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if tableView == addPlanLeftTableView {
          let leftTitleLabelModel = self.leftCreateDataSource[indexPath.row]
            if indexPath.row == 0 {
                self.current = 1
                self.getRightListData(current: self.current, positions: "")
            }else{
                self.current = 1
                self.getRightListData(current: self.current, positions: "\(indexPath.row)")
            }
        }else if tableView == addPlanRightTableView {
            let records = self.dataSource[indexPath.row]
            let traindetailPlanVc = MCTraindetailPlanVc.init()
            traindetailPlanVc.ids = records?.id ?? ""
            self.navigationController?.pushViewController(traindetailPlanVc, animated: true)
        }
    }
    
    
}
